package com.ddxz.tool.http.method;

import com.ddxz.tool.http.exception.HttpClientException;
import com.ddxz.tool.http.factory.AbstractDdxzHttpRequest;
import okhttp3.FormBody;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;

import java.io.IOException;
import java.util.Map;

/**
 * class description
 *
 * @author 朱宗刚
 * @version 1.0.0
 * @date 2020/11/30
 */

public class DdxzHttpGet extends AbstractDdxzHttpRequest {
    public DdxzHttpGet(OkHttpClient client) {
        super(client);
    }

    @Override
    public String doRequest(String url, Map headers, Object entity) {
        if(null != entity) {
            throw new HttpClientException("GET request do not contains body");
        }

        try {
            Request.Builder builder = new Request.Builder().url(url);
            if(null != headers) {
                headers.forEach((key, val) -> builder.addHeader(key.toString(), val.toString()));
            }
            return client.newCall(builder.build()).execute().body().string();
        } catch (IOException e) {
            throw new HttpClientException("error while execute request: " + url);
        }
    }
}
